package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.Member;
import com.mohe.dangjian.service.interfaces.IMember;
import com.mohe.dangjian.utils.Page;

public class MemberImpl implements IMember {

	private IDao<Member> memberDao;

	public IDao<Member> getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(IDao<Member> memberDao) {
		this.memberDao = memberDao;
	}

	@Override
	public List<Member> getMemberList(Member member, Page page, Map<String, String> paramMap) {
		StringBuffer where = new StringBuffer();
		where.append("from Member where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 姓名
		if (!StringUtils.isBlank(member.getName())) {
			where.append(" and name like ?");
			params.add("%%" + member.getName() + "%%");
		}
		// 性别
		if (!StringUtils.isBlank(member.getSex())) {
			where.append(" and sex like ?");
			params.add("%%" + member.getSex() + "%%");
		}
		// 民族
		if (!StringUtils.isBlank(member.getNationality())) {
			where.append(" and nationality like ?");
			params.add("%%" + member.getNationality() + "%%");
		}
		// 籍贯
		if (!StringUtils.isBlank(member.getNativePlace())) {
			where.append(" and nativePlace like ?");
			params.add("%%" + member.getNativePlace() + "%%");
		}
		// 出生日期
		if (!StringUtils.isBlank(member.getBorn())) {
			where.append(" and born like ?");
			params.add("%%" + member.getBorn() + "%%");
		}
//		if (!StringUtils.isBlank(minBorn)) {
//			where.append(" and born >= ?");
//			params.add(minBorn);
//		}
//		if (!StringUtils.isBlank(maxBorn)) {
//			where.append(" and born <= ?");
//			params.add(maxBorn);
//		}
		// 学历
		if (!StringUtils.isBlank(member.getDegree())) {
			// （-5）大学及以上学历（>=6）、（-4）大专及以上学历（>=5）、（-3）高中（包括中专）及以下（<=4,>=0）、（-2）初中及以下（<=1,>=0）
			if("-2".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=1");
			}else if("-3".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=4");
			}else if("-4".equals(member.getDegree())){
				where.append(" and degree >= 5");
			}else if("-5".equals(member.getDegree())){
				where.append(" and degree >= 6");
			}else{
				where.append(" and degree = ?");
				params.add(member.getDegree());
			}
		}
		// 工作日期
		if (!StringUtils.isBlank(member.getWorkDate())) {
			where.append(" and workDate like ?");
			params.add("%%" + member.getWorkDate() + "%%");
		}
		// 所属党组织
		if (!StringUtils.isBlank(member.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + member.getPartyName() + "%%");
		}
		// 党员类型
		if (!StringUtils.isBlank(member.getMemberType())) {
			where.append(" and memberType like ?");
			params.add("%%" + member.getMemberType() + "%%");
		}
		if (!StringUtils.isBlank(member.getJoinDate())) {
			where.append(" and joinDate like ?");
			params.add("%%" + member.getJoinDate() + "%%");
		}
		if (!StringUtils.isBlank(member.getConfirmDate())) {
			where.append(" and confirmDate like ?");
			params.add("%%" + member.getConfirmDate() + "%%");
		}
		// 职务名称
		if (!StringUtils.isBlank(member.getTitle())) {
			where.append(" and title like ?");
			params.add("%%" + member.getTitle() + "%%");
		}
		// 备注
		if (!StringUtils.isBlank(member.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + member.getNote() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(member.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + member.getPartyType() + "%%");
		}
		// 党总支名称
		if (!StringUtils.isBlank(member.getGeneralPartyName())) {
			where.append(" and generalPartyName like ?");
			params.add("%%" + member.getGeneralPartyName() + "%%");
		}
		if(paramMap != null){
			// 年龄段
			if (!StringUtils.isBlank(paramMap.get("minBorn"))) {
				where.append(" and born >= ?");
				params.add(paramMap.get("minBorn"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxBorn"))) {
				where.append(" and born <= ?");
				params.add(paramMap.get("maxBorn"));
			}
			// 参加工作日期段
			if (!StringUtils.isBlank(paramMap.get("minWorkDate"))) {
				where.append(" and workDate >= ?");
				params.add(paramMap.get("minWorkDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxWorkDate"))) {
				where.append(" and workDate <= ?");
				params.add(paramMap.get("maxWorkDate"));
			}
			// 入党日期段
			if (!StringUtils.isBlank(paramMap.get("minJoinDate"))) {
				where.append(" and joinDate >= ?");
				params.add(paramMap.get("minJoinDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxJoinDate"))) {
				where.append(" and joinDate <= ?");
				params.add(paramMap.get("maxJoinDate"));
			}
			// 转正日期段
			if (!StringUtils.isBlank(paramMap.get("minConfirmDate"))) {
				where.append(" and confirmDate >= ?");
				params.add(paramMap.get("minConfirmDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxConfirmDate"))) {
				where.append(" and confirmDate <= ?");
				params.add(paramMap.get("maxConfirmDate"));
			}	
		}
		
		where.append(" order by createDate desc");
		List<Member> list = memberDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Member member, Map<String, String> paramMap) {
		StringBuffer where = new StringBuffer();
		where.append("from Member where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 姓名
		if (!StringUtils.isBlank(member.getName())) {
			where.append(" and name like ?");
			params.add("%%" + member.getName() + "%%");
		}
		// 性别
		if (!StringUtils.isBlank(member.getSex())) {
			where.append(" and sex like ?");
			params.add("%%" + member.getSex() + "%%");
		}
		// 民族
		if (!StringUtils.isBlank(member.getNationality())) {
			where.append(" and nationality like ?");
			params.add("%%" + member.getNationality() + "%%");
		}
		// 籍贯
		if (!StringUtils.isBlank(member.getNativePlace())) {
			where.append(" and nativePlace like ?");
			params.add("%%" + member.getNativePlace() + "%%");
		}
		// 出生年月
		if (!StringUtils.isBlank(member.getBorn())) {
			where.append(" and born like ?");
			params.add("%%" + member.getBorn() + "%%");
		}
//		if (!StringUtils.isBlank(minBorn)) {
//			where.append(" and born >= ?");
//			params.add(minBorn);
//		}
//		if (!StringUtils.isBlank(maxBorn)) {
//			where.append(" and born <= ?");
//			params.add(maxBorn);
//		}
		// 学历
		if (!StringUtils.isBlank(member.getDegree())) {
			// （-5）大学及以上学历（>=6）、（-4）大专及以上学历（>=5）、（-3）高中（包括中专）及以下（<=4,>=0）、（-2）初中及以下（<=1,>=0）
			if("-2".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=1");
			}else if("-3".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=4");
			}else if("-4".equals(member.getDegree())){
				where.append(" and degree >= 5");
			}else if("-5".equals(member.getDegree())){
				where.append(" and degree >= 6");
			}else{
				where.append(" and degree = ?");
				params.add(member.getDegree());
			}
		}
		// 参加工作年月
		if (!StringUtils.isBlank(member.getWorkDate())) {
			where.append(" and workDate like ?");
			params.add("%%" + member.getWorkDate() + "%%");
		}
		// 所属党组织
		if (!StringUtils.isBlank(member.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + member.getPartyName() + "%%");
		}
		// 党员类型
		if (!StringUtils.isBlank(member.getMemberType())) {
			where.append(" and memberType like ?");
			params.add("%%" + member.getMemberType() + "%%");
		}
		// 入党日期
		if (!StringUtils.isBlank(member.getJoinDate())) {
			where.append(" and joinDate like ?");
			params.add("%%" + member.getJoinDate() + "%%");
		}
		// 转正日期
		if (!StringUtils.isBlank(member.getConfirmDate())) {
			where.append(" and confirmDate like ?");
			params.add("%%" + member.getConfirmDate() + "%%");
		}
		// 职务名称
		if (!StringUtils.isBlank(member.getTitle())) {
			where.append(" and title like ?");
			params.add("%%" + member.getTitle() + "%%");
		}
		// 备注
		if (!StringUtils.isBlank(member.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + member.getNote() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(member.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + member.getPartyType() + "%%");
		}
		// 党总支名称
		if (!StringUtils.isBlank(member.getGeneralPartyName())) {
			where.append(" and generalPartyName like ?");
			params.add("%%" + member.getGeneralPartyName() + "%%");
		}
		if(paramMap != null){
			// 年龄段
			if (!StringUtils.isBlank(paramMap.get("minBorn"))) {
				where.append(" and born >= ?");
				params.add(paramMap.get("minBorn"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxBorn"))) {
				where.append(" and born <= ?");
				params.add(paramMap.get("maxBorn"));
			}
			// 参加工作日期段
			if (!StringUtils.isBlank(paramMap.get("minWorkDate"))) {
				where.append(" and workDate >= ?");
				params.add(paramMap.get("minWorkDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxWorkDate"))) {
				where.append(" and workDate <= ?");
				params.add(paramMap.get("maxWorkDate"));
			}
			// 入党日期段
			if (!StringUtils.isBlank(paramMap.get("minJoinDate"))) {
				where.append(" and joinDate >= ?");
				params.add(paramMap.get("minJoinDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxJoinDate"))) {
				where.append(" and joinDate <= ?");
				params.add(paramMap.get("maxJoinDate"));
			}
			// 转正日期段
			if (!StringUtils.isBlank(paramMap.get("minConfirmDate"))) {
				where.append(" and confirmDate >= ?");
				params.add(paramMap.get("minConfirmDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxConfirmDate"))) {
				where.append(" and confirmDate <= ?");
				params.add(paramMap.get("maxConfirmDate"));
			}	
		}
		
		return memberDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveMember(Member member) {
		return memberDao.insert(member);
	}

	@Override
	public boolean saveMemberList(List<Member> memberList) {
		return memberDao.insert(memberList);
	}

	@Override
	public Member getMemberById(Long timeKey) {
		Member member = memberDao.getByKey(timeKey);
		if (member != null) {
			return member;
		}
		return null;
	}

	@Override
	public boolean updateMember(Member member) {
		return memberDao.update(member);
	}

	@Override
	public boolean delMember(Long timeKey) {
		return memberDao.delete(timeKey);
	}

	@Override
	public List<Member> getAllMember() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return memberDao.query(where.toString(), null);
	}

	@Override
	public List<Member> getMemberList(Member member, Map<String, String> paramMap) {
		StringBuffer where = new StringBuffer();
		where.append("from Member where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 姓名
		if (!StringUtils.isBlank(member.getName())) {
			where.append(" and name like ?");
			params.add("%%" + member.getName() + "%%");
		}
		// 性别
		if (!StringUtils.isBlank(member.getSex())) {
			where.append(" and sex like ?");
			params.add("%%" + member.getSex() + "%%");
		}
		// 民族
		if (!StringUtils.isBlank(member.getNationality())) {
			where.append(" and nationality like ?");
			params.add("%%" + member.getNationality() + "%%");
		}
		// 籍贯
		if (!StringUtils.isBlank(member.getNativePlace())) {
			where.append(" and nativePlace like ?");
			params.add("%%" + member.getNativePlace() + "%%");
		}
		if (!StringUtils.isBlank(member.getBorn())) {
			where.append(" and born like ?");
			params.add("%%" + member.getBorn() + "%%");
		}
		// 学历
		if (!StringUtils.isBlank(member.getDegree())) {
			// （-5）大学及以上学历（>=6）、（-4）大专及以上学历（>=5）、（-3）高中（包括中专）及以下（<=4,>=0）、（-2）初中及以下（<=1,>=0）
			if("-2".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=1");
			}else if("-3".equals(member.getDegree())){
				where.append(" and degree >= 0 and degree <=4");
			}else if("-4".equals(member.getDegree())){
				where.append(" and degree >= 5");
			}else if("-5".equals(member.getDegree())){
				where.append(" and degree >= 6");
			}else{
				where.append(" and degree = ?");
				params.add(member.getDegree());
			}
		}
		if (!StringUtils.isBlank(member.getWorkDate())) {
			where.append(" and workDate like ?");
			params.add("%%" + member.getWorkDate() + "%%");
		}
		// 所属党组织
		if (!StringUtils.isBlank(member.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + member.getPartyName() + "%%");
		}
		// 党员类型
		if (!StringUtils.isBlank(member.getMemberType())) {
			where.append(" and memberType like ?");
			params.add("%%" + member.getMemberType() + "%%");
		}
		if (!StringUtils.isBlank(member.getJoinDate())) {
			where.append(" and joinDate like ?");
			params.add("%%" + member.getJoinDate() + "%%");
		}
		if (!StringUtils.isBlank(member.getConfirmDate())) {
			where.append(" and confirmDate like ?");
			params.add("%%" + member.getConfirmDate() + "%%");
		}
		// 职务名称
		if (!StringUtils.isBlank(member.getTitle())) {
			where.append(" and title like ?");
			params.add("%%" + member.getTitle() + "%%");
		}
		// 备注
		if (!StringUtils.isBlank(member.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + member.getNote() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(member.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + member.getPartyType() + "%%");
		}
		// 党总支名称
		if (!StringUtils.isBlank(member.getGeneralPartyName())) {
			where.append(" and generalPartyName like ?");
			params.add("%%" + member.getGeneralPartyName() + "%%");
		}
		if(paramMap != null){
			// 年龄段
			if (!StringUtils.isBlank(paramMap.get("minBorn"))) {
				where.append(" and born >= ?");
				params.add(paramMap.get("minBorn"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxBorn"))) {
				where.append(" and born <= ?");
				params.add(paramMap.get("maxBorn"));
			}
			// 参加工作日期段
			if (!StringUtils.isBlank(paramMap.get("minWorkDate"))) {
				where.append(" and workDate >= ?");
				params.add(paramMap.get("minWorkDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxWorkDate"))) {
				where.append(" and workDate <= ?");
				params.add(paramMap.get("maxWorkDate"));
			}
			// 入党日期段
			if (!StringUtils.isBlank(paramMap.get("minJoinDate"))) {
				where.append(" and joinDate >= ?");
				params.add(paramMap.get("minJoinDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxJoinDate"))) {
				where.append(" and joinDate <= ?");
				params.add(paramMap.get("maxJoinDate"));
			}
			// 转正日期段
			if (!StringUtils.isBlank(paramMap.get("minConfirmDate"))) {
				where.append(" and confirmDate >= ?");
				params.add(paramMap.get("minConfirmDate"));
			}
			if (!StringUtils.isBlank(paramMap.get("maxConfirmDate"))) {
				where.append(" and confirmDate <= ?");
				params.add(paramMap.get("maxConfirmDate"));
			}	
		}

		where.append(" order by createDate desc");
		List<Member> list = memberDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
}
